﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Util
{
    /// <summary>
    /// 数学公式相关类
    /// </summary>
    public class MathHelper
    {
        /// <summary>
        /// 计算最小值
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double Min(double[] values)
        {
            if (values == null || values.Length == 0)
                return 0;

            double min = 99999999;

            foreach (double v in values)
            {
                if (min > v)
                    min = v;
            }

            return min;
        }

        /// <summary>
        /// 计算最大值
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double Max(double[] values)
        {
            if (values == null || values.Length == 0)
                return 0;

            double max = -99999999;

            foreach (double v in values)
            {
                if (max < v)
                    max = v;
            }

            return max;
        }

        /// <summary>
        /// 计算平均值
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double Average(double[] values)
        {
            if (values == null || values.Length == 0)
                return 0;

            double sum = 0;

            foreach (double v in values)
                sum += v;

            return sum / values.Length;
        }

        /// <summary>
        /// 计算标准差
        /// </summary>
        /// <returns></returns>
        public static double StdDev(double[] values)
        {
            double avg = Average(values);

            return StdDev(values, avg);
        }

        /// <summary>
        /// 计算标准差
        /// </summary>
        /// <param name="values"></param>
        /// <param name="avgValue"></param>
        /// <returns></returns>
        public static double StdDev(double[] values,double AvgValue)
        {
            double sumstdev = 0;
            foreach (double v in values)
            {
                sumstdev = sumstdev + (v - AvgValue) * (v - AvgValue);
            }

            return Math.Sqrt(sumstdev);
        }

        /// <summary>
        /// 计算波动系数
        /// </summary>
        /// <param name="StdDev"></param>
        /// <param name="AvgValue"></param>
        /// <returns></returns>
        public static double FluCoeff(double StdDev, double AvgValue)
        {
            if (AvgValue == 0)
                return 0;

            return StdDev / AvgValue;
        }
    }
}
